package ijij41.togetherTrain.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import ijij41.togetherTrain.*;
import android.app.Activity;
import android.content.*;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class TimeActivity extends Activity implements Constants {

	private TextView tv_numberOfPerson;
	private int mYear;
	private int mMonth;
	private int mDay;
	private int mHour;
	private int mMinute;

	private TextView mText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.time);

		initComponent();

	}

	private void initComponent() {

		// initial component
		(findViewById(R.id.cfm_button)).setOnClickListener(mClickListener);
		(findViewById(R.id.btn_back_img)).setOnClickListener(mClickListener);
		mText = (TextView) findViewById(R.id.tv_time);

		TimeZone tz = TimeZone.getTimeZone("GMT+09:00");
		Calendar c = Calendar.getInstance(tz);
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);

		((DatePicker) findViewById(R.id.datePicker1)).init(mYear, mMonth, mDay,
				new DatePicker.OnDateChangedListener() {
					@Override
					public void onDateChanged(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						mYear = year;
						mMonth = monthOfYear;
						mDay = dayOfMonth;

						updateDisplay();
					}
				});

		((TimePicker) findViewById(R.id.timePicker1)).setCurrentHour(mHour);
		((TimePicker) findViewById(R.id.timePicker1)).setCurrentMinute(mMinute);

		((TimePicker) findViewById(R.id.timePicker1))
				.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

					@Override
					public void onTimeChanged(TimePicker view, int hourOfDay,
							int minute) {
						mHour = hourOfDay;
						mMinute = minute;

						updateDisplay();
					}
				});

		// get parameter
		Intent intent = getIntent();
		String data = intent.getStringExtra("CAL_DATE");
		String time = intent.getStringExtra("CAL_TIME");
		setTime(time);

	}

	private void updateDisplay() {
		mText.setText(String.format("%d년 %d월 %d일 %d시 %d분", mYear, mMonth + 1,
				mDay, mHour, mMinute));

	}

	private void setTime(String time) {

		updateDisplay();

	}

	private Button.OnClickListener mClickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.btn_back_img:
				finish();
				break;

			case R.id.cfm_button:
				Bundle extra = new Bundle();
				Intent intent = new Intent();

				makeBundle(extra);
				intent.putExtras(extra);
				setResult(TIME_OK, intent);

				finish();
				break;

			default:
				break;
			}

		}

		private void makeBundle(Bundle extra) {


			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

			SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf_time = new SimpleDateFormat("HH:mm");

			Date date;
			String str_date = null;
			String str_time = null;

			try {
				date = sdf.parse(mYear + "-" + (mMonth + 1) + "-" + mDay + " "
						+ mHour + ":" + mMinute);

				str_date = sdf_date.format(date);
				str_time = sdf_time.format(date);

			} catch (ParseException e) {
				e.printStackTrace();
			}

			extra.putString("CAL_DATE", str_date);
			extra.putString("CAL_TIME", str_time);
		}

		private String getTime() {

			return null;
		}
	};

}
